<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="btnCancel">
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >
        <div class="roleName">
          {{ item.name }}
        </div>
      </el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <el-row justify="center" type="flex">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetail } from '@/api/user'
import { assignRole } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 全部角色
      list: [],
      // 已勾选角色
      checkList: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 999 })
      this.list = rows
    },
    async getUserDetail(id) {
      const res = await getUserDetail(id)
      console.log(res)
      this.checkList = [...res.roleIds]
    },
    async btnOK() {
      // 发请求
      await assignRole({
        id: this.employeeId,
        roleIds: this.checkList
      })
      // 提醒用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    btnCancel() {
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.roleName {
  margin: 4px;
}
</style>
